/*
 * @Descripttion: GGB
 * @version: 1.0
 * @Author: GGB
 * @Date: 2021-08-19 09:27:11
 * @LastEditors: GGB
 * @LastEditTime: 2021-08-19 14:09:03
 */
import 'package:flutter/material.dart';
import 'package:flutter_data_center/components/row_menu.dart';
import 'package:flutter_data_center/controller/warning_controller.dart';
import 'package:flutter_data_center/screens/warning/add/warning_add_screen.dart';
import 'package:flutter_data_center/screens/warning/detail/warning_detail_screen.dart';
import 'package:flutter_data_center/utills/color_utils.dart';
import 'package:flutter_data_center/utills/navigator_utils.dart';
import 'package:get/get.dart';
import 'package:line_icons/line_icons.dart';

import 'components/warning_list.dart';

class WaringScreen extends StatefulWidget {
  const WaringScreen({Key key}) : super(key: key);
  static String routeName = "/warning";

  @override
  _WaringScreenState createState() => _WaringScreenState();
}

class _WaringScreenState extends State<WaringScreen> {
  WarningController _warningController = Get.put(WarningController());
  final keyWidget = GlobalKey();
  int currentIndex = 0;
  double height = 0;
  void calculateSizeAndPosition() =>
      WidgetsBinding.instance.addPostFrameCallback((_) {
        final RenderBox box = keyWidget.currentContext.findRenderObject();
        setState(() {
          height = box.size.height + 90;
        });
      });
  void initState() {
    super.initState();
    calculateSizeAndPosition();
    // 发起网络请求
    _warningController.getWarnings();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: buildAppBar(),
        body: SafeArea(
          child: SingleChildScrollView(
            child: Column(
              children: [
                Padding(
                  key: keyWidget,
                  padding: EdgeInsets.symmetric(horizontal: 20),
                  child: Column(
                    children: [
                      RowMenu(
                        currentIndex: currentIndex,
                        onChange: (value) {
                          setState(() {
                            currentIndex = value;
                          });
                        },
                        activeStyle: TextStyle(
                          fontSize: 16,
                        ),
                        inactiveStyle: TextStyle(
                          fontSize: 16,
                          color: gColor("#C9C9C9"),
                        ),
                        menus: _warningController.menus,
                        showRight: true,
                        slot: IconButton(
                          icon: Icon(LineIcons.plus),
                          onPressed: () {
                            NavigatorUtils.pushPageByFade(
                              context: context,
                              targetPage: WarningAddScreen(),
                              isReplace: false,
                              startMills: 500,
                            );
                          },
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 10, vertical: 0),
                  height: MediaQuery.of(context).size.height - height,
                  decoration: BoxDecoration(
                    color: gColor("#E5E5E5"),
                  ),
                  child: WarningList(
                    onTap: (item) {
                      NavigatorUtils.pushPageByFade(
                        context: context,
                        targetPage: WarningDetailScreen(warning: item),
                        isReplace: false,
                        startMills: 500,
                      );
                    },
                  ),
                ),
              ],
            ),
          ),
        ),
      );

  AppBar buildAppBar() {
    return AppBar(
      backgroundColor: Colors.transparent,
      title: Text(
        "校园隐患",
        style: TextStyle(
          fontSize: 18,
        ),
      ),
      elevation: 0,
    );
  }
}
